/**
 * 弹出消息框JS
 * author:liyi
 * date:2012-07-11
 * 
 */

/**
 * 显示消息框
 * popWinId id
 * beforeFun 显示前执行function
 * showAlign 显示方式 sourceObjLeftBown(事件源对象左下) center (居中)
 */
function showPopWin(targetObj,popWinId,beforeFun,showAlign){
	var popObj = $("#"+popWinId);
	if(showAlign == "sourceObjLeftBown"){
		var top=$(targetObj).offset().top; 
		var left=$(targetObj).offset().left; 
		popObj.css({'top':top+20,'left':left});
	}else if(showAlign =="center"){
		var wnd = $(window), doc = $(document);  
	    var left = doc.scrollLeft();  
	    var top = doc.scrollTop();  
	    left += (wnd.width() - popObj.width())/2;  
	    top += (wnd.height() - popObj.height())/2;  
	    popObj.css("top",top);  
	    popObj.css("left",left);  
	}
	
	var rVal = beforeFun;//执行方法
	if( -1 != rVal){
		popObj.show();
		$("#keepDiv").show();
    }
}

/**
 * 关闭消息窗
 * @param popWinId
 */
function hidePopWin(popWinId){
	var popObj = $("#"+popWinId);
	popObj.hide();
	$("#keepDiv").hide();
}


/**
 * 复制到剪贴板
 * id input id
 */
function copyToClipBoardByTextId(id){
	copy2Clipboard($("#"+id).val());
}
/**
 * 复制到剪贴板
 * clipBoardContent 内容
 */
copy2Clipboard=function(txt){
	if(window.clipboardData){
		window.clipboardData.clearData();
		window.clipboardData.setData("Text",txt);
	}
	else if(navigator.userAgent.indexOf("Opera")!=-1){
		window.location=txt;
	}
	else if(window.netscape){
		try{
			netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
		}
		catch(e){
			alert("\u60a8\u7684firefox\u5b89\u5168\u9650\u5236\u9650\u5236\u60a8\u8fdb\u884c\u526a\u8d34\u677f\u64cd\u4f5c\uff0c\u8bf7\u6253\u5f00\u2019about:config\u2019\u5c06signed.applets.codebase_principal_support\u2019\u8bbe\u7f6e\u4e3atrue\u2019\u4e4b\u540e\u91cd\u8bd5\uff0c\u76f8\u5bf9\u8def\u5f84\u4e3afirefox\u6839\u76ee\u5f55/greprefs/all.js");
			return false;
		}
		var clip=Components.classes['@mozilla.org/widget/clipboard;1'].createInstance(Components.interfaces.nsIClipboard);
		if(!clip)return;
		var trans=Components.classes['@mozilla.org/widget/transferable;1'].createInstance(Components.interfaces.nsITransferable);
		if(!trans)return;
		trans.addDataFlavor('text/unicode');
		var str=new Object();
		var len=new Object();
		var str=Components.classes["@mozilla.org/supports-string;1"].createInstance(Components.interfaces.nsISupportsString);
		var copytext=txt;str.data=copytext;
		trans.setTransferData("text/unicode",str,copytext.length*2);
		var clipid=Components.interfaces.nsIClipboard;
		if(!clip)return false;
		clip.setData(trans,null,clipid.kGlobalClipboard);
		
		alert("\u5df2\u7ecf\u590d\u5236\u5230\u526a\u5207\u677f!");
	}
}

/**
 * 求长度 英文算0.5 中文1
 * @returns
 */
String.prototype.len=function(){
	var length = this.length;
	var sum = 0;
	for(var i = 0 ; i < length ; i++){
		if((this.charCodeAt(i)>=0)&&(this.charCodeAt(i)<=255)){  
			sum=sum+0.5;   
		}else{   
			sum=sum+1;   
		}    
	}
	return sum;
}


String.prototype.Trim = function() 
{ 
	return this.replace(/(^\s*)|(\s*$)/g, ""); 
} 
String.prototype.LTrim = function() 
{ 
	return this.replace(/(^\s*)/g, ""); 
} 
String.prototype.RTrim = function() 
{ 
	return this.replace(/(\s*$)/g, ""); 
} 

/**
 * 弹出ALERT层
 * title:标题
 * message:消息内容
 * outTime:退出时间 -1表示永不自动关闭
 */
function alertDiv(title,message,outTime){
	var alterDivObj = $("#alterDiv");
	if(!title || title ==""){
		title = "\u6e29\u99a8\u63d0\u793a"; //温馨提示
	}
	$("#alterDiv H4 SPAN").html(title);
	$("#alterDiv .bd2 P").html(message);
	
	 
	showPopWin('','alterDiv',alertBeforeFun(outTime),'center');
}
/**
 * 弹出ALERT层(IMG)
 * 
 * outTime:退出时间 -1表示永不自动关闭
 */
function alertImg(id,outTime){
	showPopWin('',id,alertImgBeforeFun(id,outTime),'center');
	
}
function alertBeforeFun(outTime){
	$("#alterDiv .close").unbind("click");
	$("#alterDiv .close").bind("click",function(){
		outTime = -1;
		$("#alterDiv").hide();
		$("#keepDiv").hide();
   		return false;
	});
	//自动关闭
	if(outTime>0){
		
		setTimeout(function(){
			$("#alterDiv").hide();
			$("#keepDiv").hide();
		},outTime);
	}
}

function alertImgBeforeFun(id,outTime){
	
	//自动关闭
	if(outTime>0){
		
		setTimeout(function(){
			$("#"+id).hide();
			$("#keepDiv").hide();
		},outTime);
	}
}



/********************************************************************************************/


/**
 * 分享活动
 * @param period状态
 * @param active_subject 活动标题
 * @param join_amount 参加人数
 * @param start_time 开始时间
 */
function shareActivitieFun(roomName,roomId,period,active_subject,join_amount,start_time){
	
	 var data={
			    url:"http://zy.17guagua.com/room/share.jsp?roomName="+encodeURIComponent(roomName)+"&roomid="+roomId,
				title:""
	 };
	//正在进行
	if(period == "1"){
		data.title ="\u6211\u6b63\u5728\u53c2\u52a0\u5471\u5471\u6d3b\u52a8\u4e2d\u5fc3\u7684\u300a"+(active_subject)+"\u300b\uff0c\u5f88\u7cbe\u5f69\uff0c\u76ee\u524d\u6709"+join_amount+"\u4eba\u6b63\u5728\u73b0\u573a\uff01\u5feb\u6765\u548c\u6211\u4e00\u8d77\u53c2\u52a0\u5427\uff01";
	}else if(period == "3" || period == "4" || period == "5"){
		data.title =start_time+"\uff0c\u5927\u5bb6\u548c\u6211\u4e00\u8d77\u6765\u53c2\u52a0\u5471\u5471\u6d3b\u52a8\u4e2d\u5fc3\u7684\u300a"+(active_subject)+"\u300b\u5427\uff01";
	}else if(period == "2"){//活动结束
		data.title ="\u6211\u53c2\u52a0\u4e86\u5471\u5471\u6d3b\u52a8\u4e2d\u5fc3\u7684\u300a"+(active_subject)+"\u300b\u5f88\u7cbe\u5f69\u54e6!";
		
	}else{
		return -1;
	}
	
	//停止轮播
//	alert(st);
	 if(st){
		 st.Stop(); 
	 }
//	 var roomURL = $("#shareActivitiePop INPUT").val();
	
		
	 $("#shareActivitiePop_copy").unbind("click");
	 $("#shareActivitiePop .SINA_WEBO_A").unbind("click");
	 $("#shareActivitiePop .QQ_ZONE_A").unbind("click");
	 $("#shareActivitiePop .QQ_WEIBO_A").unbind("click");
	 $("#shareActivitiePop .RENREN_A").unbind("click");
	 $("#shareActivitiePop .KAIXIN_A").unbind("click");
	 $("#shareActivitiePop .DOUBA_A").unbind("click");
	 
	$("#shareActivitiePop_copy").click(function(){
		copy2Clipboard($("#shareActivitiePop INPUT").val());
		return false;
	});
	
	$("#shareActivitiePop .SINA_WEBO_A").click(function (){
		var url = getShareURL('SINA_WEIBO',data);
		window.open (url, '', 'height=500, width=800, top=0, left=0, toolbar=no, menubar=no, scrollbars=yes,resizable=yes,location=no, status=yes') ;
		hidePopWin('shareActivitiePop');//关闭窗口
		if(st){
			 st.Run(); 
		 }
		return false;
	});
	$("#shareActivitiePop .QQ_ZONE_A").click(function (){
		var url = getShareURL('QQ_ZONE',data);
		window.open (url, '', 'height=500, width=800, top=0, left=0, toolbar=no, menubar=no, scrollbars=yes,resizable=yes,location=no, status=yes') ;
		hidePopWin('shareActivitiePop');//关闭窗口
		if(st){
			 st.Run(); 
		 }
		return false;
	});
	$("#shareActivitiePop .QQ_WEIBO_A").click(function (){
		var url = getShareURL('QQ_WEIBO',data);
		window.open (url, '', 'height=500, width=800, top=0, left=0, toolbar=no, menubar=no, scrollbars=yes,resizable=yes,location=no, status=yes') ;
		hidePopWin('shareActivitiePop');//关闭窗口
		if(st){
			 st.Run(); 
		 }
		return false;
	});
	$("#shareActivitiePop .RENREN_A").bind("click",(function (){
		var url = getShareURL('RENREN',data);
		window.open (url, '', 'height=500, width=800, top=0, left=0, toolbar=no, menubar=no, scrollbars=yes,resizable=yes,location=no, status=yes') ;
		hidePopWin('shareActivitiePop');//关闭窗口
		if(st){
			 st.Run(); 
		 }
		return false;
	}));
	$("#shareActivitiePop .KAIXIN_A").bind("click",(function (){
		var url = getShareURL('KAIXIN',data);
		window.open (url, '', 'height=500, width=800, top=0, left=0, toolbar=no, menubar=no, scrollbars=yes,resizable=yes,location=no, status=yes') ;
		hidePopWin('shareActivitiePop');//关闭窗口
		if(st){
			 st.Run(); 
		 }
		return false;
	}));
	$("#shareActivitiePop .DOUBA_A").bind("click",(function (){
		var url = getShareURL('DOUBA',data);
		window.open (url, '', 'height=500, width=800, top=0, left=0, toolbar=no, menubar=no, scrollbars=yes,resizable=yes,location=no, status=yes') ;
		hidePopWin('shareActivitiePop');//关闭窗口
		if(st){
			 st.Run(); 
		 }
		return false;
	}));
}


/**
 * 分享活动(更多活动页面）
 */
function shareActivitie2Fun(roomName,roomId,period,active_subject,join_amount,start_time){
	
	 var data={
			    url:"http://zy.17guagua.com/room/share.jsp?roomName="+encodeURIComponent(roomName)+"&roomid="+roomId,
				title:""
	 };
	//正在进行
	if(period == "1"){
		data.title ="\u6211\u6b63\u5728\u53c2\u52a0\u5471\u5471\u6d3b\u52a8\u4e2d\u5fc3\u7684\u300a"+(active_subject)+"\u300b\uff0c\u5f88\u7cbe\u5f69\uff0c\u76ee\u524d\u6709"+join_amount+"\u4eba\u6b63\u5728\u73b0\u573a\uff01\u5feb\u6765\u548c\u6211\u4e00\u8d77\u53c2\u52a0\u5427\uff01";
	}else if(period == "3" || period == "4" || period == "5"){
		data.title =start_time+"\uff0c\u5927\u5bb6\u548c\u6211\u4e00\u8d77\u6765\u53c2\u52a0\u5471\u5471\u6d3b\u52a8\u4e2d\u5fc3\u7684\u300a"+(active_subject)+"\u300b\u5427\uff01";
	}else if(period == "2"){//活动结束
		data.title ="\u6211\u53c2\u52a0\u4e86\u5471\u5471\u6d3b\u52a8\u4e2d\u5fc3\u7684\u300a"+(active_subject)+"\u300b\u5f88\u7cbe\u5f69\u54e6!";
		
	}else{
		return -1;
	}
		
	 $("#shareActivitiePop_copy").unbind("click");
	 $("#shareActivitiePop .SINA_WEBO_A").unbind("click");
	 $("#shareActivitiePop .QQ_ZONE_A").unbind("click");
	 $("#shareActivitiePop .QQ_WEIBO_A").unbind("click");
	 $("#shareActivitiePop .RENREN_A").unbind("click");
	 $("#shareActivitiePop .KAIXIN_A").unbind("click");
	 $("#shareActivitiePop .DOUBA_A").unbind("click");
	 
	$("#shareActivitiePop_copy").click(function(){
		copy2Clipboard($("#shareActivitiePop INPUT").val());
	});
	
	$("#shareActivitiePop .SINA_WEBO_A").click(function (){
		var url = getShareURL('SINA_WEIBO',data);
		window.open (url, '', 'height=500, width=800, top=0, left=0, toolbar=no, menubar=no, scrollbars=yes,resizable=yes,location=no, status=yes') ;
		hidePopWin('shareActivitiePop');//关闭窗口
		return false;
		
	});
	$("#shareActivitiePop .QQ_ZONE_A").click(function (){
		var url = getShareURL('QQ_ZONE',data);
		window.open (url, '', 'height=500, width=800, top=0, left=0, toolbar=no, menubar=no, scrollbars=yes,resizable=yes,location=no, status=yes') ;
		hidePopWin('shareActivitiePop');//关闭窗口
		return false;
		
	});
	$("#shareActivitiePop .QQ_WEIBO_A").click(function (){
		var url = getShareURL('QQ_WEIBO',data);
		window.open (url, '', 'height=500, width=800, top=0, left=0, toolbar=no, menubar=no, scrollbars=yes,resizable=yes,location=no, status=yes') ;
		hidePopWin('shareActivitiePop');//关闭窗口
		return false;
		
	});
	$("#shareActivitiePop .RENREN_A").bind("click",(function (){
		var url = getShareURL('RENREN',data);
		window.open (url, '', 'height=500, width=800, top=0, left=0, toolbar=no, menubar=no, scrollbars=yes,resizable=yes,location=no, status=yes') ;
		hidePopWin('shareActivitiePop');//关闭窗口
		return false;
	}));
	$("#shareActivitiePop .KAIXIN_A").bind("click",(function (){
		var url = getShareURL('KAIXIN',data);
		window.open (url, '', 'height=500, width=800, top=0, left=0, toolbar=no, menubar=no, scrollbars=yes,resizable=yes,location=no, status=yes') ;
		hidePopWin('shareActivitiePop');//关闭窗口
		return false;
	}));
	$("#shareActivitiePop .DOUBA_A").bind("click",(function (){
		var url = getShareURL('DOUBA',data);
		window.open (url, '', 'height=500, width=800, top=0, left=0, toolbar=no, menubar=no, scrollbars=yes,resizable=yes,location=no, status=yes') ;
		hidePopWin('shareActivitiePop');//关闭窗口
		return false;
	}));
}

/**
 * 分享房间
 * 
 */
function shareRoomFun(roomName,roomId){
//	var roomURL = $("#shareRoomPop INPUT").val();
	var data={
	    url:"http://zy.17guagua.com/room/share.jsp?roomName="+encodeURIComponent(roomName)+"&roomid="+roomId,
		title:"\u5927\u5bb6\u597d\uff0c\u6211\u6b63\u5728\u5471\u5471\u89c6\u9891\u793e\u533a\u89c2\u770b\u300a"+roomName+"\u300b\u7684\u7cbe\u5f69\u76f4\u64ad\uff0c\u9080\u8bf7\u5927\u5bb6\u5f3a\u52bf\u56f4\u89c2\u3002 \uff08\u5206\u4eab\u81ea @\u5471\u5471\u89c6\u9891\u793e\u533a\uff09"
	};
	
	$("#shareRoomPop_copy").unbind("click");
	$("#shareRoomPop .SINA_WEBO_A").unbind("click");
	$("#shareRoomPop .QQ_ZONE_A").unbind("click");
	$("#shareRoomPop .QQ_WEIBO_A").unbind("click");
	$("#shareRoomPop .RENREN_A").unbind("click");
	$("#shareRoomPop .KAIXIN_A").unbind("click");
	$("#shareRoomPop .DOUBA_A").unbind("click");
	
	//绑定事件
	$("#shareRoomPop_copy").bind("click",(function(){
		copy2Clipboard($("#shareRoomPop INPUT").val());
	}));
	
	$("#shareRoomPop .SINA_WEBO_A").bind("click",(function (){
		var url = getShareURL('SINA_WEIBO',data);
		window.open (url, '', 'height=500, width=800, top=0, left=0, toolbar=no, menubar=no, scrollbars=yes,resizable=yes,location=no, status=yes') ;
		hidePopWin('shareRoomPop');//关闭窗口
		return false;
	}));
	$("#shareRoomPop .QQ_ZONE_A").bind("click",(function (){
		var url = getShareURL('QQ_ZONE',data);
		window.open (url, '', 'height=500, width=800, top=0, left=0, toolbar=no, menubar=no, scrollbars=yes,resizable=yes,location=no, status=yes') ;
		hidePopWin('shareRoomPop');//关闭窗口
		return false;
	}));
	$("#shareRoomPop .QQ_WEIBO_A").bind("click",(function (){
		var url = getShareURL('QQ_WEIBO',data);
		window.open (url, '', 'height=500, width=800, top=0, left=0, toolbar=no, menubar=no, scrollbars=yes,resizable=yes,location=no, status=yes') ;
		hidePopWin('shareRoomPop');//关闭窗口
		return false;
	}));
	$("#shareRoomPop .RENREN_A").bind("click",(function (){
		var url = getShareURL('RENREN',data);
		window.open (url, '', 'height=500, width=800, top=0, left=0, toolbar=no, menubar=no, scrollbars=yes,resizable=yes,location=no, status=yes') ;
		hidePopWin('shareRoomPop');//关闭窗口
		return false;
	}));
	$("#shareRoomPop .KAIXIN_A").bind("click",(function (){
		var url = getShareURL('KAIXIN',data);
		window.open (url, '', 'height=500, width=800, top=0, left=0, toolbar=no, menubar=no, scrollbars=yes,resizable=yes,location=no, status=yes') ;
		hidePopWin('shareRoomPop');//关闭窗口
		return false;
	}));
	$("#shareRoomPop .DOUBA_A").bind("click",(function (){
		var url = getShareURL('DOUBA',data);
		window.open (url, '', 'height=500, width=800, top=0, left=0, toolbar=no, menubar=no, scrollbars=yes,resizable=yes,location=no, status=yes') ;
		hidePopWin('shareRoomPop');//关闭窗口
		return false;
	}));
	
}

/**
 * 分享返回分享URL
 * shareType (SINA微博 QQ空间。。。。)
 */
function getShareURL(shareType,data){
	var shareUrl="";
	var sinaWEIBO_URL = "http://service.weibo.com/share/share.php?type=3&url=[url]&appkey=[appkey]&title=[title]&pic=&ralateUid=&language=zh_cn";
	var qqZoneURL ="http://sns.qzone.qq.com/cgi-bin/qzshare/cgi_qzshare_onekey?url=[url]&title=[title]&pics=&summary=";
	var qqWEIBO_URL ="http://share.v.t.qq.com/index.php?c=share&a=index&title=[title]&url=[url]&appkey=[appkey]&site=&pic=";
	var douba_URL="http://shuo.douban.com/!service/share?image=&href=[url]&name=[title]";
	var kaixin_URL="http://www.kaixin001.com/rest/records.php?content=[title]&url=[url]&aid=100018706&style=11&pic=&t=27";
	var renren_URL="http://widget.renren.com/dialog/forward?url=[url]&title=title&image=image&content=[title]";
	if(shareType == "SINA_WEIBO"){
		shareUrl = sinaWEIBO_URL;
	}else if(shareType == "QQ_ZONE"){
		shareUrl = qqZoneURL;
	}else if(shareType == "QQ_WEIBO"){
		shareUrl = qqWEIBO_URL;
	}else if(shareType == "RENREN"){
		shareUrl = renren_URL;
	}else if(shareType == "KAIXIN"){
		shareUrl = kaixin_URL;
	}else if(shareType == "DOUBA"){
		shareUrl = douba_URL;
	}
	
	
	if(data){
		if(data.url){
			shareUrl = shareUrl.replace("[url]", encodeURIComponent(data.url)); 
		}else{
			shareUrl = shareUrl.replace("[url]", ""); 
		}
		if(data.appkey){
			shareUrl = shareUrl.replace("[appkey]", encodeURIComponent(data.appkey)); 
		}else{
			shareUrl = shareUrl.replace("[appkey]", ""); 
		}
		if(data.title){
			shareUrl = shareUrl.replace("[title]", encodeURIComponent(data.title)); 
		}else{
			shareUrl = shareUrl.replace("[title]", ""); 
		}
	}else{
		shareUrl = shareUrl.replace("[url]", ""); 
		shareUrl = shareUrl.replace("[appkey]", ""); 
		shareUrl = shareUrl.replace("[title]", ""); 
	}
//	alert(shareUrl);
	//shareUrl = encodeURI(shareUrl);
	return shareUrl;
	
}